package com.yuanluludev.lib_pickerview.utils

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import com.yuanluludev.lib_pickerview.R
import com.yuanluludev.lib_pickerview.builder.TimePickerBuilder
import com.yuanluludev.lib_pickerview.listener.OnTimeSelectChangeListener
import com.yuanluludev.lib_pickerview.listener.OnTimeSelectListener
import com.yuanluludev.lib_pickerview.view.TimePickerView
import com.yuanluludev.lib_utils.util.TimeUtils
import java.util.*


object TimerPickerUtils {

    enum class TimeType {
        //年月日
        YEAR_MONTH_DAY,

        //年月日时分
        YEAR_MONTH_DAY_HOUR_MIN,

        //年月日时分秒
        YEAR_MONTH_DAY_HOUR_MIN_SECOND,

        //时分秒,
        HOUR_MIN_SECOND
    }

    fun showTimePicker(
        context: Context,
        listener: OnTimeSelectListener,
        timeType: TimeType = TimeType.YEAR_MONTH_DAY_HOUR_MIN,
        //默认开始时间  2000年1月1日 0时0分0秒
        startMills: Long = 946656000000,
        endMills: Long = TimeUtils.getNowMills(),
        currentMills: Long = TimeUtils.getNowMills(),
        cancelText: String = "取消",
        submitText: String = "确定",
        titleText: String = "请选择",
        outSideCancelable: Boolean = true,
        contentTextSize: Int = 16,
        titleSize: Int = 18,
        isDialog: Boolean = false,
        isCyclic: Boolean = false,
    ) {

        val startDate = TimeUtils.dateToCalendar(TimeUtils.millis2Date(startMills))
        val endDate = TimeUtils.dateToCalendar(TimeUtils.millis2Date(endMills))
        val currentDate = TimeUtils.dateToCalendar(TimeUtils.millis2Date(currentMills))

        TimePickerBuilder(
            context,
            listener
        ).apply {
            when (timeType) {
                TimeType.YEAR_MONTH_DAY -> {
                    setType(booleanArrayOf(true, true, true, false, false, false))
                }
                TimeType.YEAR_MONTH_DAY_HOUR_MIN -> {
                    setType(booleanArrayOf(true, true, true, true, true, false))
                }
                TimeType.YEAR_MONTH_DAY_HOUR_MIN_SECOND -> {
                    setType(booleanArrayOf(true, true, true, true, true, true))
                }
                TimeType.HOUR_MIN_SECOND -> {
                    setType(booleanArrayOf(false, false, false, true, true, true))
                }
            }
            setCancelText(cancelText) //取消按钮文字
            setSubmitText(submitText) //确认按钮文字
            setContentTextSize(contentTextSize)
            setTitleSize(titleSize) //标题文字大小
            setTitleText(titleText) //标题文字
            setOutSideCancelable(outSideCancelable) //点击屏幕，点在控件外部范围时，是否取消显示
            isCyclic(isCyclic) //是否循环滚动
            setDate(currentDate)
            setRangDate(startDate, endDate)
            setTitleColor(Color.parseColor("#333333")) //标题文字颜色
            setSubmitColor(Color.parseColor("#555555")) //确定按钮文字颜色
            setCancelColor(Color.parseColor("#555555")) //取消按钮文字颜色
            setTitleBgColor(Color.WHITE) //标题背景颜色 Night mode
            setBgColor(Color.WHITE) //滚轮背景颜色 Night mode
            setLabel("年", "月", "日", "时", "分", "秒") //默认设置为年月日时分秒
            isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            isDialog(isDialog) //是否显示为对话框样式
            build().show()
        }

    }

    /**
     * 以view的形式展示 时间选择
     */
    fun showTimePickerWithDecorView(
        context: Context,
        viewGroup: ViewGroup,
        listener: OnTimeSelectChangeListener,
        timeType: TimeType = TimeType.YEAR_MONTH_DAY,
        currentMills: Long = TimeUtils.getNowMills(),
        rangeStartDate: Calendar = Calendar.getInstance().apply { set(2010, 1, 1) },
        rangeEndDate: Calendar = Calendar.getInstance().apply { set(2023, 1, 1) },
    ): TimePickerView {
        TimePickerBuilder(context) { date, _ ->

        }.setLayoutRes(R.layout.pickerview_custom_time) {

        }.apply {
            when (timeType) {
                TimeType.YEAR_MONTH_DAY -> {
                    setType(booleanArrayOf(true, true, true, false, false, false))
                }
                TimeType.YEAR_MONTH_DAY_HOUR_MIN -> {
                    setType(booleanArrayOf(true, true, true, true, true, false))
                }
                TimeType.YEAR_MONTH_DAY_HOUR_MIN_SECOND -> {
                    setType(booleanArrayOf(true, true, true, true, true, true))
                }
                TimeType.HOUR_MIN_SECOND -> {
                    setType(booleanArrayOf(false, false, false, true, true, true))
                }
            }

            val currentDate: Calendar =
                Calendar.getInstance().apply { time = TimeUtils.millis2Date(currentMills) }

            setDate(currentDate)
            setTimeSelectChangeListener(listener)
            setRangDate(rangeStartDate, rangeEndDate)
            setDecorView(viewGroup)

            return build().apply {
                setKeyBackCancelable(false)
                show(false)
            }

        }
    }

}